from data.Datagrama import SimpleDatagram, IPDatagram
from engine.Assembler import mountSimpleDatagram, getImportantPorts
from engine.Processor import layerRealTimeStatistics, printLayerStatistics, \
    transportLayerStatistics, printTransportLayerStatistics, portStatistics, \
    printPortStatistics, savePortStatistics, saveTransportLayerStatistics, \
    saveLayerStatistics, broadcastStatistics, printBroadcastStatistics, \
    saveBroadcastStatistics, subnetOriginStatistics, printSubnetOriginStatistics, \
    saveSubnetOriginStatistics
import Processor
import sys

def usage():
    print "annalyzer <input file> <output_file>"

if __name__ == '__main__':
    
    if len(sys.argv) < 3:
        usage()
        sys.exit(0)

    file = sys.argv[1]
    output = sys.argv[2]
    f = open(file,"r")

    layers = []
    layers_count = []
    ports , ports_count = getImportantPorts("data/important-ports.lst")
    transport = [0,0]
    total = 0
    broacast = 0
    subnets = []
    subnets_count = []
    
    for line in f:
        if line == "\n":
            print "Linha em branco"
        else:
            total += 1
            datagram = mountSimpleDatagram(line)
            layers,layers_count = layerRealTimeStatistics(datagram,layers,layers_count)
            ports,ports_count = portStatistics(datagram,ports,ports_count)
            transport = transportLayerStatistics(datagram,transport)
            broacast = broadcastStatistics(datagram,broacast)
            subnets,subnets_count = subnetOriginStatistics(datagram,24,subnets,subnets_count)
            
    
    printLayerStatistics(layers,layers_count,total)
    saveLayerStatistics(output,layers,layers_count,total)
    
    printPortStatistics(ports,ports_count,total)
    savePortStatistics(output,ports,ports_count,total)
    
    printTransportLayerStatistics(transport,total)
    saveTransportLayerStatistics(output,transport,total)
    
    printBroadcastStatistics(broacast,total)
    saveBroadcastStatistics(output,broacast,total)
    
    printSubnetOriginStatistics(subnets,subnets_count)
    saveSubnetOriginStatistics(output,subnets,subnets_count)
    
    